package com.hfut.eeg.service;

import com.github.pagehelper.PageHelper;
import com.hfut.eeg.common.util.GMT;
import com.hfut.eeg.common.util.GenerateID;
import com.hfut.eeg.dao.PatientMapper;
import com.hfut.eeg.dto.DiagnoseDatePatient;
import com.hfut.eeg.dto.PatientListResult;
import com.hfut.eeg.dto.PatientSingleParam;
import com.hfut.eeg.dto.QueryPatientsParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.service
 * @Description: TODO
 * @date 2019-11-05 18:42
 */
@Service
@Scope("singleton")
public class PatientInfoService implements GenerateID {

    @Autowired
    private PatientMapper patient;

    //根据页标签获取病人列表信息
    public List<PatientListResult> getPatientList(Integer pageSize, Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        return format(patient.getPatientList());
    }

    //根据搜索条件获取病人列表信息
    public List<PatientListResult> queryPatientsList(QueryPatientsParam param,Integer pageSize, Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        return format(patient.queryPatients(param));
    }

    //根据病人id删除病人信息
    public int deletePatientById(String id,String checkTime){
        //System.out.println(checkTime);
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("checkTime", Timestamp.valueOf(checkTime));
        return patient.deletePatientById(map);
    }

    //添加病人
    public int addPatient(PatientSingleParam param){
        return patient.addPatient(param);
    }

    //查看待就诊病人
    public HashMap<String,Object> getDiagnoseDatePatient(){
        //获取当前日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());

        //对今日病人数据进行归类整理
        List<DiagnoseDatePatient> patients = patient.getDatePatient(date);
        HashMap<String,Object> result = new HashMap<>();

        //存储已检查、未检查病人信息数据
        ArrayList<DiagnoseDatePatient> notCheckPatiet = new ArrayList<>();
        ArrayList<DiagnoseDatePatient> checkedPatient = new ArrayList<>();
        for (DiagnoseDatePatient patientItem:patients){
            //0表示未检查、1表示已检查
            if(patientItem.getCheckStatus()==0){
                notCheckPatiet.add(patientItem);
            }else {
                checkedPatient.add(patientItem);
            }
        }
        result.put("notCheckNum",notCheckPatiet.size());
        result.put("checkedNum",checkedPatient.size());
        result.put("notCheckPatiet",notCheckPatiet);
        result.put("checkedPatient",checkedPatient);
        return result;
    }

    //修改病人状态信息
    public int changePatientStatus(String patientId){
        HashMap<String,Object> map = new HashMap();
        map.put("status",1);//修改病人为已检查状态
        map.put("patientId",patientId);
        return patient.changePatientStatus(map);
    }

    //更新病人检查时间
    public int updatePatientCheckTime(String patientId){
        HashMap<String,Object> map = new HashMap();
        map.put("checkTime",new Timestamp(System.currentTimeMillis()));
        map.put("patientId",patientId);
        return patient.updatePatientCheckTime(map);
    }

    //更新病人检查花费时间
    public  int updatePatientCostTime(String patientId){
        //先获取情人开始检查的时间
        Timestamp timestamp = patient.getPatientCheckTime(patientId);
        Long current = System.currentTimeMillis();
        //算出分钟
        int costTime = (int) ((current-timestamp.getTime())/(1000*60));
        HashMap<String,Object> map = new HashMap();
        map.put("costTime",costTime);
        map.put("patientId",patientId);
        return patient.updatePatientCostTime(map);
    }

    public PatientListResult getPatientById(String id){
        return patient.getPatientById(id);
    }

    //获取最新病人记录的ID，用来实现主键生成策略
    public String getLatestID(){
        return patient.getPatientLatestID();
    }

    public String getNameById(String Id){
       return patient.getNameById(Id);
    }

    //格式化时间
    private List<PatientListResult> format(List<PatientListResult> patients){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        patients.stream().map((patient)->trans(patient)).collect(Collectors.toList());
        //System.out.println(patients);
        return patients;
    }

    //设置了前端需要显示的时间格式
    private PatientListResult trans(PatientListResult patientListResult){
        String timestamp = patientListResult.getCheckTime();
        patientListResult.setShowTime(timestamp.split(" ")[0]);
        return patientListResult;
    }

    //更新病人异常波数量
    public int updateAbWavesNum(String patientId,int num){
        HashMap map = new HashMap();
        map.put("patientId",patientId);
        map.put("num",num);
        return patient.updateAbWavesNum(map);
    }

    public PatientListResult getPatientByEEG(Long eegId){
        return patient.getPatientByEEG(eegId);
    }

    //获取病人的最新eegId
    public Long getPatientLatestEEGId(String patientId){
        return patient.getPatientLatestEEGId(patientId);
    }

    public List<PatientListResult> getAllPatients(){
        return patient.getAllPatientChecked();
    }
}
